/*
 @description: 网点

 @author: 李建翰

 @update: 
*/
'use strict';
const Controller = require('egg').Controller;

class NetworkController extends Controller {
    // 查询
    async index() { //搜索
        const {ctx,app} = this;
        const {keyname,page=1,number=9} = ctx.request.query
        const offset=(page-1)*number
        let data,count
        if(keyname && page){
            data = await app.mysql.query(`select id,area,city,spot from fuxiao_network where city like '%${keyname}%' limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_network where city like '%${keyname}%'`)
        }else if(keyname){
            data = await app.mysql.query(`select id,area,city,spot from fuxiao_network where city ='%${keyname}%' or city  like '%${keyname}%' limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_network where city like '%${keyname}%' or city like '%${keyname}%'`)
        }else{
            data=await app.mysql.query(`select id,area,city,spot from fuxiao_network limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_network`)
        }

        
    // async index() { //搜索
    //     const {ctx,app} = this;
    //     const {keyname} = ctx.request.query
    //     let where=''
    //     if(keyname){
    //         where=`where ador_name like "%${keyname}%"`
    //     }
    //     const result =await app.mysql.query(`select * from fuxiao_administrators ${where}`)
        ctx.body = {
            code:0,
            msg:"查找成功",
            data,
            count:count[0].count
        }
    }

    async new() { //搜索
        const {ctx,app} = this;
        const {keyname,page=1,number=9} = ctx.request.query
        const offset=(page-1)*number
        let data,count
        if(keyname && page){
            data = await app.mysql.query(`select id,area,city,spot from fuxiao_network where area like '%${keyname}%' limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_network where area like '%${keyname}%'`)
        }else if(keyname){
            data = await app.mysql.query(`select id,area,city,spot from fuxiao_network where area ='%${keyname}%' or area  like '%${keyname}%' limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_network where area like '%${keyname}%' or area like '%${keyname}%'`)
            
        }else{
            data=await app.mysql.query(`select id,area,city,spot from fuxiao_network limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_network`)
        }

        
    // async index() { //搜索
    //     const {ctx,app} = this;
    //     const {keyname} = ctx.request.query
    //     let where=''
    //     if(keyname){
    //         where=`where ador_name like "%${keyname}%"`
    //     }
    //     const result =await app.mysql.query(`select * from fuxiao_administrators ${where}`)
        ctx.body = {
            code:0,
            msg:"查找成功",
            data,
            count:count[0].count
        }
    }

    async show(){
        const {ctx,app} = this;
        const {id} = ctx.params;
        const result=await app.mysql.get('fuxiao_network',{ID:id})
        if(result){
            ctx.body={
                code:0,
                msg:'查找成功',
                data:result
            };
        }else{
            ctx.body = {
                code:4007,
                msg:"查找失败"
            }
        }
    }

            
            // 添加
            async create(){     //post
                const { ctx,app} = this;
                const {area,city,spot}=ctx.request.body
                if(area && spot &&city){
                    const data=await app.mysql.insert('fuxiao_network',{area,city,spot});
                    ctx.body = {
                        code: 0,
                        msg: "新增成功",
                    };
                } else {
                    ctx.body = {
                        code: 4006,
                        msg: "添加失败",
                    };
                }
            }
            //改
            async update() {//put
                const {ctx,app} =this;
                const {area,city,spot} = ctx.request.body
                const {id} = ctx.params;
                const row={
                    area:area,
                    spot:spot,
                    city:city
                };
                const options={
                    where:{
                        id:+id
                    }
                };
                const result=await app.mysql.update('fuxiao_network',row,options);
                    if(result){
                        ctx.body={
                            code:0,
                            msg:"编辑成功",
                        };
                    }else{
                        ctx.body={
                            code:4008,
                            msg:"编辑失败",
                        };
                    } 
            }

            //删除
            async destroy() { //DELETE
                const {ctx,app} = this;
                const {id} = ctx.params;
                const result =await app.mysql.delete(`fuxiao_network`,{id:+id})
                if(result){
                    ctx.body = {
                        code: 0,
                        msg: "删除成功",
                    };
                }else{
                    ctx.body = {
                        code: 4007,
                        msg: "删除失败",
                    }; 
                }
                }
}
module.exports = NetworkController;